/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.viewlists;

import com.google.common.collect.Lists;
import listo.client.model.Folder;
import listo.client.model.ObjectId;
import listo.client.model.Task;
import listo.client.model.Tasks;

import java.util.List;
import java.util.Map;

/**
 * Represents a folder node in the left hand side view tree.
 */
public class FolderView extends View {

    private static Tasks selectedTasks;

    private Folder folder;
    private FolderView parent;
    private final List<FolderView> subViews = Lists.newArrayList();

    public void setFolder(Folder folder, Map<ObjectId, Tasks> map) {
        this.folder = folder;
        Tasks tasks = map.get(folder.getId());
        setTaskCount(tasks != null ? tasks.size() : 0);

        // create sub views
        subViews.clear();
        List<Folder> subFolders = folder.getFolders();
        if (subFolders != null) {
            for (Folder subFolder : subFolders) {
                FolderView subView = new FolderView();
                subView.setFolder(subFolder, map);
                subViews.add(subView);
                subView.parent = this;
            }
        }
    }

    public static void setSelectedTasks(Tasks selectedTasks) {
        FolderView.selectedTasks = selectedTasks;
    }

    public Folder getFolder() {
        return folder;
    }

    public FolderView getParent() {
        return parent;
    }

    public List<FolderView> getSubViews() {
        return subViews;
    }

    public boolean contains(FolderView view) {
        if (this == view) return true;
        if (subViews != null) {
            for (FolderView subView : subViews) {
                if (subView.contains(view)) return true;
            }
        }
        return false;
    }

    /**
     * Collects the sub tree underneath this node into the given map in a recursive
     * depth-first tree walk. This node itself is included first.
     *
     * @param views the viewl list to collect into
     */
    public void collectSubTree(List<FolderView> views) {
        views.add(this);
        if (subViews != null) {
            for (FolderView subView : subViews) {
                subView.collectSubTree(views);
            }
        }
    }

    public void render(ViewListRenderer renderer, boolean isSelected, boolean hasFocus, boolean isDrop) {
        super.render(renderer, isSelected, hasFocus, isDrop);
        Folder folder = getFolder();
        renderer.setText(folder.getName());
        renderer.setIcon(parent != null ? "16x16/folders/" + Folder.getIcon(folder.getIcon()) : null);
        renderer.setLevel(getLevel());
        if (selectedTasks != null) {
            for (Task selectedTask : selectedTasks) {
                if (selectedTask.hasFolders(folder.getId())) {
                    renderer.setBold(true);
                    return;
                }
            }
        }
        renderer.setBold(false);
    }

    private int getLevel() {
        return parent == null ? 0 : parent.getLevel() + 1;
    }

}